<template>
  <uni-popup ref="popupRef" background-color="#fff" @change="change">
    <view style="width: 90vw; padding: 40rpx 40rpx 80rpx;">
      <view class="form-title">流程转办</view>
      <view style="padding-bottom: 80rpx">
        <pm-user-select v-model:user-id="userId"></pm-user-select>
      </view>
      <view class="mt10">
        <d-button type="primary" @click="handleTransfer">确认转办</d-button>
        <d-button class="mt10" type="white" @click="close">关闭</d-button>
      </view>
    </view>
  </uni-popup>
</template>

<script setup lang="ts">

import {usePmBpmApi} from "@/api/pm/bpm";
import {useRouteHistoryStore} from "@/stores/routeHistory";

defineOptions({ name: 'ProcessTransferPopup' })

const userId = ref<number>(null);
const popupRef = ref();
let taskId = null;
function open(id: string) {
  taskId = id;
  userId.value = null;
  if (popupRef.value) {
    popupRef.value.open('center')
  }
}

function close() {
  if (popupRef.value) {
    popupRef.value.close()
  }
}

function handleTransfer() {
  if (taskId) {
    usePmBpmApi().updateTaskAssignee({
      id: taskId,
      assigneeUserId: userId.value,
    }).then(res => {
      if (res.code === 0 && res.data) {
        uni.showToast({
          icon: 'success',
          title: '转办成功'
        })
        useRouteHistoryStore().routeGoBack();
      } else {
        uni.showToast({
          icon: 'error',
          title: `转办失败，${res.msg}`
        })
      }
    })
  }
}

defineExpose({
  open
})
</script>

<style scoped lang="scss">

</style>
